package com.edu.exam.entity.examination.control;

import com.edu.exam.enums.DetectionStatus;
import com.edu.exam.enums.common.SeverityLevel;
import com.edu.exam.enums.examination.control.AnomalyType;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 异常检测实体类
 * 所属模块：E-01 考试执行模块
 * 对应数据库表：anomaly_detection
 *
 * <p>异常检测实体用于记录和管理安全监控过程中的异常检测结果，包括
 * 异常识别、分析、评估、处理等。支持完整的异常检测流程管理。</p>
 *
 * <p>核心功能：
 * - 异常识别：识别和记录各类异常
 * - 异常分析：深入分析异常的特征和原因
 * - 异常评估：评估异常的严重程度和影响
 * - 异常处理：制定和执行异常处理方案
 * - 趋势分析：支持异常趋势分析</p>
 *
 * @author System
 * @version 1.0.0
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
@EntityListeners(AuditingEntityListener.class)
public class AnomalyDetection {

    /**
     * 主键ID
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    /**
     * 检测ID
     *
     * <p>异常检测的唯一标识符，采用UUID格式生成。</p>
     */
    @Column(name = "detection_id", nullable = false, length = 32, unique = true)
    private String detectionId;

    /**
     * 任务ID
     *
     * <p>关联的监控任务ID。</p>
     */
    @Column(name = "task_id", nullable = false, length = 32)
    private String taskId;

    /**
     * 考试会话ID
     *
     * <p>关联的考试会话ID。</p>
     */
    @Column(name = "session_id", nullable = false, length = 32)
    private String sessionId;

    /**
     * 异常类型
     *
     * <p>异常的类型。
     * 支持的类型：BEHAVIOR（行为）、SYSTEM（系统）、NETWORK（网络）、PERFORMANCE（性能）、SECURITY（安全）等</p>
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "anomaly_type", nullable = false, length = 50)
    private AnomalyType anomalyType;

    /**
     * 异常子类型
     *
     * <p>异常的子类型。</p>
     */
    @Column(name = "anomaly_subtype", length = 50)
    private String anomalySubtype;

    /**
     * 严重级别
     *
     * <p>异常的严重程度级别。
     * 支持的级别：LOW（低）、MEDIUM（中）、HIGH（高）、CRITICAL（关键）、FATAL（致命）</p>
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "severity_level", nullable = false, length = 20)
    private SeverityLevel severityLevel;

    /**
     * 异常评分
     *
     * <p>异常的综合评分，范围0-10。</p>
     */
    @Column(name = "anomaly_score", precision = 4, scale = 2)
    private Float anomalyScore = 0.0f;

    /**
     * 置信度
     *
     * <p>检测结果的置信度，范围0-1。</p>
     */
    @Column(name = "confidence_level", precision = 3, scale = 2)
    private Float confidenceLevel = 0.0f;

    /**
     * 检测时间
     *
     * <p>异常被检测到的时间。</p>
     */
    @Column(name = "detection_time", nullable = false)
    private LocalDateTime detectionTime;

    /**
     * 检测状态
     *
     * <p>异常检测的状态。
     * 支持的状态：DETECTED（已检测）、ANALYZING（分析中）、ANALYZED（已分析）、PROCESSED（已处理）、CLOSED（已关闭）</p>
     */
    @Column(name = "detection_status", nullable = false, length = 20)
    private String detectionStatus = DetectionStatus.DETECTED.getCode();

    /**
     * 检测算法ID
     *
     * <p>用于检测的算法ID。</p>
     */
    @Column(name = "algorithm_id", length = 32)
    private String algorithmId;

    /**
     * 检测算法名称
     *
     * <p>用于检测的算法名称。</p>
     */
    @Column(name = "algorithm_name", length = 100)
    private String algorithmName;

    /**
     * 异常描述
     *
     * <p>异常的详细描述。</p>
     */
    @Column(name = "anomaly_description", columnDefinition = "TEXT", length = 1000)
    private String anomalyDescription;

    /**
     * 异常特征
     *
     * <p>异常的主要特征。</p>
     */
    @Column(name = "anomaly_features", columnDefinition = "TEXT", length = 1000)
    private String anomalyFeatures;

    /**
     * 影响范围
     *
     * <p>异常的影响范围。</p>
     */
    @Column(name = "impact_scope", length = 500)
    private String impactScope;

    /**
     * 影响程度
     *
     * <p>异常的影响程度评分，范围0-10。</p>
     */
    @Column(name = "impact_score", precision = 3, scale = 1)
    private Float impactScore = 0.0f;

    /**
     * 频率评分
     *
     * <p>异常发生的频率评分，范围0-10。</p>
     */
    @Column(name = "frequency_score", precision = 3, scale = 1)
    private Float frequencyScore = 0.0f;

    /**
     * 分析者ID
     *
     * <p>分析异常的操作者ID。</p>
     */
    @Column(name = "analyzer_id", length = 32)
    private String analyzerId;

    /**
     * 分析时间
     *
     * <p>异常被分析的时间。</p>
     */
    @Column(name = "analysis_time")
    private LocalDateTime analysisTime;

    /**
     * 分析结果
     *
     * <p>异常分析的结果。</p>
     */
    @Column(name = "analysis_result", columnDefinition = "TEXT", length = 1000)
    private String analysisResult;

    /**
     * 处理方案
     *
     * <p>异常处理的方案。</p>
     */
    @Column(name = "treatment_plan", columnDefinition = "TEXT", length = 1000)
    private String treatmentPlan;

    /**
     * 处理者ID
     *
     * <p>处理异常的操作者ID。</p>
     */
    @Column(name = "handler_id", length = 32)
    private String handlerId;

    /**
     * 处理时间
     *
     * <p>异常被处理的时间。</p>
     */
    @Column(name = "handling_time")
    private LocalDateTime handlingTime;

    /**
     * 处理结果
     *
     * <p>异常处理的结果。</p>
     */
    @Column(name = "handling_result", length = 1000)
    private String handlingResult;

    /**
     * 是否误报
     *
     * <p>标记是否为误报。</p>
     */
    @Column(name = "is_false_positive")
    private Boolean isFalsePositive = false;

    /**
     * 误报原因
     *
     * <p>误报的原因说明。</p>
     */
    @Column(name = "false_positive_reason", length = 500)
    private String falsePositiveReason;

    /**
     * 异常标签
     *
     * <p>JSON格式的异常标签。</p>
     */
    @Column(name = "anomaly_tags", columnDefinition = "TEXT")
    private String anomalyTags;

    /**
     * 异常备注
     *
     * <p>异常的备注信息。</p>
     */
    @Column(name = "anomaly_notes", columnDefinition = "TEXT")
    private String anomalyNotes;

    /**
     * 基准值
     *
     * <p>用于比较的基准值。</p>
     */
    @Column(name = "baseline_value", precision = 15, scale = 6)
    private BigDecimal baselineValue;

    /**
     * 观测值
     *
     * <p>实际观测到的值。</p>
     */
    @Column(name = "observed_value", precision = 15, scale = 6)
    private BigDecimal observedValue;

    /**
     * 偏差率
     *
     * <p>观测值相对于基准值的偏差率。</p>
     */
    @Column(name = "deviation_rate", precision = 5, scale = 2)
    private BigDecimal deviationRate;

    /**
     * 阈值
     *
     * <p>异常检测的阈值。</p>
     */
    @Column(name = "threshold_value", precision = 15, scale = 6)
    private BigDecimal thresholdValue;

    // ===== 审计字段 =====

    /**
     * 创建时间
     */
    @CreatedDate
    @Column(name = "create_time", nullable = false, updatable = false)
    private LocalDateTime createTime;

    /**
     * 更新时间
     */
    @LastModifiedDate
    @Column(name = "update_time", nullable = false)
    private LocalDateTime updateTime;

    /**
     * 版本号
     */
    @Column(name = "version", nullable = false)
    private Integer version = 1;

    /**
     * 是否删除
     */
    @Column(name = "is_deleted", nullable = false)
    private Boolean isDeleted = false;

    // ===== 业务方法 =====
}